package com.mmall.example.singleton;

import com.mmall.annoations.Recommend;
import com.mmall.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * 改造后的饿汉模式-1
 * 枚举类型的单例，最安全最简单
 * 参考：https://www.jianshu.com/p/eb30a388c5fc?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io
 * Created by megan on 2018/3/22.
 */
@Slf4j
@ThreadSafe
@Recommend
public class SingletonExample4 {
    /**
     * 私有构造函数
     */
    private SingletonExample4() {
        // 可能会进行很多操作，很多运算
    }

    /**
     * 静态工厂方法 * @return
     */
    public static SingletonExample4 getInstance() {
        return Singleton.INSTANCE.getSingletonExample4();
    }

    /**
     * 由枚举类创建单例对象
     */
    @Getter
    private enum Singleton {
        INSTANCE;

        private SingletonExample4 singletonExample4;

        Singleton() {
            singletonExample4 = new SingletonExample4();
        }
    }
}

